package com.example.xloong.mymvvm.view;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.LinearLayout;

import com.example.xloong.mymvvm.R;
import com.example.xloong.mymvvm.adapter.NewsAdapter;
import com.example.xloong.mymvvm.base.IBaseView;
import com.example.xloong.mymvvm.databinding.ActivityMainBinding;
import com.example.xloong.mymvvm.viewmodel.NewsViewmodel;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

public class MainActivity extends AppCompatActivity implements IBaseView,XRecyclerView.LoadingListener {


    private ActivityMainBinding activityMainBinding;
    private NewsAdapter mNewsAdapter;
    private NewsViewmodel mNewsViewmodel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        mNewsAdapter = new NewsAdapter(this);
        mNewsViewmodel =new NewsViewmodel(this,mNewsAdapter);

    }

    private void initView() {
        activityMainBinding.reNews.setRefreshProgressStyle(ProgressStyle.BallClipRotate);//设置下拉刷新的样式
        activityMainBinding.reNews.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);//设置上拉加载的样式
        activityMainBinding.reNews.setArrowImageView(R.mipmap.ic_launcher);
        activityMainBinding.reNews.setLoadingListener(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        activityMainBinding.reNews.setLayoutManager(layoutManager);
        mNewsAdapter = new NewsAdapter(this);
        activityMainBinding.reNews.setAdapter(mNewsAdapter);
    }

    @Override
    public void onRefresh() {
        //下拉刷新
        mNewsViewmodel.loadRefreshData();
    }

    @Override
    public void onLoadMore() {
        //上拉加载更多
        mNewsViewmodel.loadMoreData();
    }

    @Override
    public void loadStart(int loadType) {

    }

    @Override
    public void loadComplete() {
        activityMainBinding.reNews.loadMoreComplete();//结束加载
        activityMainBinding.reNews.refreshComplete();//结束刷新
    }

    @Override
    public void loadFailure(String message) {
        activityMainBinding.reNews.loadMoreComplete();//结束加载
        activityMainBinding.reNews.refreshComplete();//结束刷新

    }
}
